package editro.view 
{
	import editro.events.system.SystemEvent;
	import editro.events.view.DrawFloateLineEvent;
	import editro.events.view.ChangePanelEvent;
	import editro.model.AntTargetVO;
	import editro.model.AntTaskVOListModel;
	import editro.model.ATECacheModel;
	import editro.model.ATEConfigModel;
	import editro.model.BindingVO;
	import editro.view.components.AntTaskView;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import nl.demonsters.debugger.MonsterDebugger;
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * ...
	 * @author LEEF LEE_LEAF918@163.COM_QQ969442267
	 */
	public class AntTaskViewMediator extends Mediator 
	{
		
		[Inject] public var view:AntTaskView;
		[Inject] public var cachemodel:ATECacheModel;
		[Inject] public var voModel:AntTaskVOListModel;
		public function AntTaskViewMediator() 
		{
			
		}
		/**
		 * robot override
		 */
		override public function onRegister():void 
		{
			var vo:AntTargetVO = new AntTargetVO;
			vo.targetName = 'ATE:'+voModel.getTasksLength();
			view.init(vo,ATEConfigModel.ATEViewItemWidth, ATEConfigModel.ATEViewItemHeight, ATEConfigModel.ATEViewItemBaseColor,ATEConfigModel.ATEViewItemArchorNumber);
			view.dragingSignal.add(onDragHandler);
			view.archorSelectedSignal.add(onSelectedArchorHandler);
			eventMap.mapListener(view, MouseEvent.DOUBLE_CLICK, doubleHandler);
			//
			voModel.pushTaskItem(view);
			super.onRegister();
		}
		
		
		/**
		 * 接收第几个ARCHOR被点击了,是一个在场景中的坐标,分为不同的情况处理
		 * @param	v
		 */
		private function onSelectedArchorHandler(v:uint):void 
		{
			
			if (cachemodel.isOnFloatDrawingModel) {
				//('处于动态连线，找到了,连接两个点', v);
				cachemodel.isOnFloatDrawingModel = false;
				if (voModel.getATETaskBinging(view.vo,cachemodel.dragingFloatLineStartedTaskVO)) 
				{
					trace('两个TASK间已经存在关系，不可以增加关系');
				}else {
					//
					voModel.pushBindingLink(cachemodel.dragingFloatLineStartedTaskVO, cachemodel.dragingFloatLineStartedTaskVOArchorIndex, view.vo, v);
				}
				//系统刷新事件
				dispatch(new SystemEvent(SystemEvent.REFRESHOVERLAYER));
			}else {
				//开始浮动连线
				cachemodel.isOnFloatDrawingModel = true;
				var bindVO:BindingVO = voModel.getBindingLink(view.vo, v);
				if (bindVO == null) {
					//此点没有与其他点关连,此点为起点，开始浮动线
					cachemodel.dragingFloatLineStartedTaskVO = view.vo;
					cachemodel.dragingFloatLineStartedTaskVOArchorIndex = v;
				}else {
					//此点与其他点关连,此点关系的另一端为起点，开始浮动线
					//先解除此点的关系线
					voModel.removeBindingLink(bindVO);
					if (bindVO._linkStartTartgetVO==view.vo) 
					{
						cachemodel.dragingFloatLineStartedTaskVO = bindVO._linkEndTargetVO;
						cachemodel.dragingFloatLineStartedTaskVOArchorIndex = bindVO._rearIndex;
					}else {
						cachemodel.dragingFloatLineStartedTaskVO = bindVO._linkStartTartgetVO;
						cachemodel.dragingFloatLineStartedTaskVOArchorIndex = bindVO._preIndex;
					}
				}
				
				dispatch(new DrawFloateLineEvent(DrawFloateLineEvent.START_DRAW_FLOATING_LINE));
			}
		}
		
		private function doubleHandler(e:MouseEvent):void 
		{
			//TODO:go to dispatch.
			//dispatch(new ChangePanelEvent(ChangePanelEvent.SHOW_TASK_DETAIL, view.vo));
		}
		
		private function onDragHandler():void 
		{
			dispatch(new SystemEvent(SystemEvent.REFRESHOVERLAYER));
		}
	}

}